//
//  BSImageCell.m
//  BS
//
//  Created by lanou on 15/8/15.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BSImageCell.h"

#import "UILabel+LJLabel.h"

#import "UIImageView+WebCache.h"

#import "Header.h"

#import "BSImageVC.h"

@implementation BSImageCell


- (void)setModel:(BSListModel *)model{
    NSLog(@"%@",model.t);
    _model = model;
    _contentLabel.text = model.text;
    //自定义行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_model.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];//调整行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [model.text length])];
    _contentLabel.attributedText = attributedString;
    [_contentLabel sizeToFit];
    
//    if ([_model.is_gif isEqualToString:@"1"]) {
//           [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
//    }
  
        [_imaView sd_setImageWithURL:[NSURL URLWithString:_model.cdn_img] placeholderImage:nil options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            _pregressLabel.hidden = NO;
            _pregressLabel.text = [NSString stringWithFormat:@"正在玩命加载%ld%%",(receivedSize * 100/ expectedSize)];
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            _pregressLabel.hidden = YES;
        }];
    if(((SCREEN_WIDTH - 2 * 8) / [model.width floatValue] * [model.height floatValue]) >= (SCREEN_HEIGHT) && [model.is_gif isEqualToString:@"0"]){
        _bigButton.hidden = NO;
    }
    else{
        _bigButton.hidden = YES;
    }
    self.clipsToBounds = YES;
//    [[SDImageCache sharedImageCache] setShouldDecompressImages:NO];
//    [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];
}

- (IBAction)playBigImage:(UIButton *)sender {
    
    BSImageVC *imageVC = [[BSImageVC alloc] initWithNibName:@"BSImageVC" bundle:nil];
    imageVC.model = _model;
    [self.vc presentViewController:imageVC animated:YES completion:nil];
}

+ (CGFloat)heightOfCell:(BSListModel *)model{
    
    if (model.cellHeight == 0){
    CGFloat height = [UILabel heightOfLabelByStr:model.text fontSize:17 width:SCREEN_WIDTH - 2 * 8 lineSpacing:5] + 16 + ((SCREEN_WIDTH - 2 * 8) / [model.width floatValue] * [model.height floatValue]);
        model.cellHeight = height > (SCREEN_HEIGHT) ? SCREEN_HEIGHT : (height);
    }
    return model.cellHeight;
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
